/**
 * 
 */
package cc.monggo.util.filesystem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author fangjinsong
 *
 */
public class CashCard {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CashCard cc = new CashCard();

		String root = "C:\\Users\\Administrator\\Desktop\\QQ邮箱\\100封测试邮件\\";
		String cashCardPath = root + "1353823287051.txt";
		String emailPath = root + "广东-深圳300.txt";
		String outPutPath = root + "out.xls";
		ArrayList emailList = cc.readEmail(emailPath);
		
		Map map = new TreeMap();
		map.put("01邮箱", emailList);
		map.putAll(cc.readCashCard(cashCardPath,emailList.size()));

		System.out.println(emailList.size());
		ExcelUtils.writeExcelToFileSystem(outPutPath, map);
	}
	
	/**
	 * 读取卡号和验证码到列表，分别存放
	 * @param path
	 * @return
	 */
	public Map readCashCard(String path,int number){
		Map map = new HashMap();
		ArrayList<String> ids = new ArrayList<String>();
		ArrayList<String> masks = new ArrayList<String>();
		File file = new File(path);
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String temp = br.readLine();
			int line = 0;
			while(temp != null){
				if(line>0){
					String idM[] = temp.split(",");
					ids.add(idM[0]);
					masks.add(idM[1]);
				}
				temp = br.readLine();
				line++;
			}
			
			//铺满整个map
			int realNum = ids.size();
			int multiple = number/realNum + 1;
			for(int i=0;i<multiple;i++){
				if(i>0){
					if(number < (i+1)*realNum){
						int remain = number - i*realNum;
						for(int j=0;j<remain;j++){
							String id = ids.get(j) + "";
							String mask = masks.get(j) + "";
							ids.add(id);
							masks.add(mask);
						}
					}else{
						for(int j=0;j<realNum;j++){
							String id = ids.get(j) + "";
							String mask = masks.get(j) + "";
							ids.add(id);
							masks.add(mask);
						}
					}
				}
			}
			
			map.put("02卡号", ids);
			map.put("03验证码", masks);
			return map;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 读取邮箱到列表中
	 * @param path
	 * @return
	 */
	public ArrayList<String> readEmail(String path){
		ArrayList<String> list = new ArrayList<String>();
		File file = new File(path);
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String temp = br.readLine();
			while(temp != null){
				list.add(temp + "");
				temp = br.readLine();
			}
			return list;			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static void createEmailCard(String path){
		
	}
}
